﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ReadWriteCsv;
using System.Text.RegularExpressions;

namespace StockAnomalyDetector
{
    class SchrodingerCat : Pattern
    {
        private List<int> hight;
        private List<int> low;
        private double prePrice;
        const double fraction = 0.015;
        private int rowsCount;
        private double diff;

        public static List<string> exceptionList = new List<string>() { "stockcode,date,time|"};
        public static CsvFileWriter writer = new CsvFileWriter(@"D:\股票数据\" + "SchrodingerCat" + ".csv");

        public SchrodingerCat(CsvRow r) : base(r)
        {
            prePrice = -1;
            hight = new List<int>();
            low = new List<int>();
            rowsCount = 0;
            diff = 0;
        }

        public override void Analyze()
        {
            double cond;
            ParseRow();
            rowsCount++;
            if (prePrice == -1)
                prePrice = price;
            diff = price - prePrice;
            if (diff > 0)
            {
                cond = (prePrice * fraction > 0.05) ? prePrice * fraction : 0.05;
                if (diff > cond)
                    hight.Add(time);
            }
            else
            {
                cond = (prePrice * fraction < -0.05) ? prePrice* fraction : -0.05;
                if (diff < cond)
                    low.Add(time);
            }
            prePrice = price;
            
        }

        public override void End(string sourceName)
        {
            if ((hight.Count + low.Count) > 500 && rowsCount*0.1< (hight.Count + low.Count))
            {
                StringBuilder s = new StringBuilder();
                s.Append(Convert.ToString(Regex.Match(sourceName, @"[A-Z]{2}\d{6}"))+",");
                s.Append(Convert.ToString(Regex.Match(sourceName, @"\d{8}"))+",");
                for (int i = 0; i < hight.Count; i++)
                    s.Append(hight[i].ToString() + "|");
                s.Append("0");
                for (int i = 0; i < low.Count; i++)
                    s.Append("|" +low[i].ToString());
                exceptionList.Add(s.ToString());
                Console.WriteLine(s.ToString());
            }

        }
    }
}
